/*密码/短信登录切换*/
var $modal_title = $(".modal-title>a");
var $login_pwd_main = $(".login_pwd_main");
var $login_note_main = $(".login_note_main");
$login_note_main.hide();
$modal_title.click(function() {
	$(this).addClass("pwd_active").siblings().removeClass("pwd_active");
	var index = $(this).index();
	switch(index) {
		case 0:
			$login_pwd_main.show();
			$login_note_main.hide();
			break;
		case 1:
			$login_pwd_main.hide();
			$login_note_main.show();
			break;
		default:
			break;
	};
});



// 获取验证码事件
var codeOnOff=true;
var second=60;
var timer=null;	
function getCodeEls(event){	
	var txtNoraml=$('.get-code');
	var getCode=$('.id-code');
	var verifKeyVal='';
	var mobileVal = $('.mobile').val();
	if(mobileVal != ''){
    	if (/^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(mobileVal)) {
    		$('.tips-form').html('');
        }
        else {
        	$('.tips-form').html('请填写正确的手机号！');
			var that = this;
			setTimeout(function(){
				$('.tips-form').html('');
			},3000);
        	return;
        }
    }
    else{
    	$('.tips-form').html('请填写手机号！');
    	var that = this;
		setTimeout(function(){
			$('.tips-form').html('');
		},3000);
        return;
    }
	if(codeOnOff){
    	txtNoraml.hide();
    	getCode.css('display','inline-block');
    	getCode.html(second+'s后重试');
		var dataCode = {
			phone:mobileVal,
			sign: "string",
			type:'REGISTER_VERIFICATION_CODE',
			smsChannel: "domestic",
		};
		$.ajax({
			url:doMain+'/api/verificationCode',
			type:"post",
			dataType:'json',
			contentType: "application/json",
			data:JSON.stringify(dataCode),
			success:function (datas) {
				// console.log('code成功');
			 	// console.log(datas)
                if(datas.code==0){
                	txtNoraml.hide();
    				getCode.css('display','inline-block');
                    getCode.addClass('active');
                    getCode.html(second+'s后重试');
                    codeOnOff=false;                    
                    // verifKeyVal = datas.data.verifKey;
                    timer=setInterval(function(){
                            if(second<=0){
                                clearInterval(timer);
                                txtNoraml.css('display','inline-block');
    							getCode.hide();
                                getCode.removeClass('active');
                                txtNoraml.html('获取验证码');
                                getCode.html('');
                                second=60;
                                codeOnOff=true; 
                            }else{
                                  second-=1;
                                  txtNoraml.hide();
    							  getCode.css('display','inline-block');  
                                  getCode.html(second+'s后重试');
                            }
                    },1000)
                }
                else if(datas.msg=='手机号已注册!'){
                	txtNoraml.css('display','inline-block');
					getCode.hide();
                    getCode.removeClass('active');
                    txtNoraml.html('获取验证码');
                    getCode.html('');
                    $('.tips-form').html(datas.msg);
                	var that = this;
					setTimeout(function(){
						$('.tips-form').html('');
					},3000);
                }
                else{
                	$('.tips-form').html(datas.msg);
                	var that = this;
					setTimeout(function(){
						$('.tips-form').html('');
					},3000);
                }
			},         
			error: function(res) {
				console.log('失败');
				$('.tips-form').html(res.responseJSON.msg);
				var that = this;
				setTimeout(function(){
					$('.tips-form').html('');
				},3000);
			},
		})
	}
}
// 注册事件
function registerEls(){
	var mobileVal = $('.mobile').val();
	var secondVal = $('.code').val();
	var passwordVal = $('.password').val();
	var referralVal = $('.referral').val();
	if(mobileVal != ''){
    	if (/^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(mobileVal)) {
    		$('.tips-form').html('');
        }
        else {
        	$('.tips-form').html('请填写正确的手机号！');
			var that = this;
			setTimeout(function(){
				$('.tips-form').html('');
			},3000);
        	return;
        }
    }
    else{
    	$('.tips-form').html('请填写手机号！');
    	var that = this;
		setTimeout(function(){
			$('.tips-form').html('');
		},3000);
        return;
    }
    if(secondVal == ''){
    	$('.tips-form').html('请填写验证码！');
    	var that = this;
		setTimeout(function(){
			$('.tips-form').html('');
		},3000);
        return;
    }
    if(referralVal == ''){
    	$('.tips-form').html('请填写邀请手机号吗！');
    	var that = this;
		setTimeout(function(){
			$('.tips-form').html('');
		},3000);
        return;
    }
    if(passwordVal == ''){
    	$('.tips-form').html('请填写密码！');
    	var that = this;
		setTimeout(function(){
			$('.tips-form').html('');
		},3000);
        return;
    }
    var serviceCheck = $('input[type="checkbox"].service-check').prop('checked');
    if(serviceCheck==false){
    	$('.tips-form').html('请阅读用户注册《服务条款》，并同意！');
    	var that = this;
		setTimeout(function(){
			$('.tips-form').html('');
		},3000);
    	return;
    }
	var dataCode = {
		  password: passwordVal,
		  phone: mobileVal,
		  referral_code: referralVal,
		  sign: "string",
		  user_id: 0,
		  verification_code: secondVal				
	};
	$('.loading').fadeIn();
	$.ajax({
		url:doMain+'/api/register',
		type:"post",
		dataType:'json',
		contentType: "application/json",
		data:JSON.stringify(dataCode),
		success:function (datas) {
			// console.log('code成功');
			// console.log(datas)
			$('.loading').hide();
			if(datas.code==0){				
				$('.mobile').val('');
				$('.code').val('');
				$('.password').val('');
				$('.referral').val('');
				$('.register-form').hide();
				$('.register-success').show();
			}
			else{
				$('.tips-form').html(datas.msg);
				var that = this;
				setTimeout(function(){
					$('.tips-form').html('');
				},3000);
			}
		},         
		error: function(res) {
			console.log('失败');
			$('.tips-form').html(res.responseJSON.msg);
			var that = this;
			setTimeout(function(){
				$('.tips-form').html('');
			},3000);
		},
	})
}
// 登录事件
function loginEls(){
	var telVal = $('.tel-login').val();
	var pwVal = $('.pw-login').val();
	if(telVal=='' || pwVal==''){
		$('.tips-form').html('登录信息不能为空！');
		var that = this;
		setTimeout(function(){
			$('.tips-form').html('');
		},3000);
	}
	else{
		var dataCode = {
			  password: pwVal,
			  phone: telVal,
			  sign: "string",
			  user_id: 0,			
		};
		$('.loading').fadeIn();	
		$.ajax({
			url:doMain+'/api/login',
			type:"post",
			dataType:'json',
			contentType: "application/json",
			data:JSON.stringify(dataCode),
			success:function (datas) {
				// console.log('code成功');
				// console.log(datas)
				$('.loading').hide();
				if(datas.code==0){
					$('.login-form').hide();
					$('.login-success').show();
					$('.tel-login').val('');
					$('.pw-login').val('');
					var that = this;
					setTimeout(function(){
						var login = $("#loginModal");
						login.hide();
						login.modal('hide');
						$('.login-form').show();
						$('.login-success').hide();
					},1500);
					localStorage.setItem('__USERMALLID__', datas.data.id);
					localStorage.setItem('__USERINFO__', JSON.stringify(datas.data));					
					$('.log.user-tel').html(JSON.parse(localStorage.__USERINFO__).phone);
					if(isLogin()==true){
						$('.logout-box').show();
						$('.login-box').hide();
						$('#laytype-2').show();
						$('#laytype-1').hide();					
					}
					var userId=localStorage.__USERMALLID__||'';
					var dataCode = {
						  sign: "string",
						  user_id: userId,			
					};
					$('.loading').fadeIn();	
					$.ajax({
						url:doMain+'/api/userAccount',
						type:"post",
						dataType:'json',
						contentType: "application/json",
						data:JSON.stringify(dataCode),
						success:function (datas) {
							localStorage.setItem('__USERPRICE_', datas.resp.preDeposit);
						},         
						error: function(res) {
							console.log('预存款失败');
						},
					})
				}
				else{
					$('.tips-form').html(datas.msg);
					var that = this;
					setTimeout(function(){
						$('.tips-form').html('');
					},5000);
				}
			},         
			error: function(res) {
				console.log('失败');
				$('.tips-form').html(res.responseJSON.msg);
				var that = this;
				setTimeout(function(){
					$('.tips-form').html('');
				},3000);
			},
		})
	}	
}
// 去登录
function goLoginEls(){
	var rigster = $("#registeredModal");
	rigster.hide();
	rigster.modal('hide');
}

// 注册服务条框 打开
function registerService(){
	$('.register-apply').show();
	var serviceHtml = '';
	$.get('register-service.html',function(html){
	    // console.log(html);
	    $('.register-apply .layer-show-article').html(html);
	});
	
}
function serviceSure(){
	$('input[type="checkbox"].service-check').prop('checked', true);
	$('.register-apply').hide();
}
function serviceHide(){
	$('.register-apply').hide();
}


// 注册层触发打开
function rigsterLayer(){
	var txtNoraml=$('.get-code');
	var getCode=$('.id-code');
	$('.mobile').val('');
	$('.code').val('');
	$('.password').val('');
	$('.referral').val('');
	$('.register-form').show();
	$('.register-success').hide();
	clearInterval(timer);
	txtNoraml.css('display','inline-block');
	getCode.hide();
	getCode.removeClass('active');
	txtNoraml.html('获取验证码');
	getCode.html('');
	second=60;
	codeOnOff=true;
	// $('input[type="checkbox"].service-check').prop('checked')
}
// 去注册
function rigsterGoLayer(){
	var login = $("#loginModal");
	login.hide();
	login.modal('hide');
}
function rigsterClose(){
	window.location.reload();
}
function loginLayer(){
	$('.loading').hide();
	$('.login_pwd_main').show();
	$('.login_note_main').hide();
	pwLogin();
}
function loginHideLayer(){
	var login = $("#loginModal");
	login.hide();
	login.modal('hide');
}
function pwLogin(){
	$('.login_pwd_main').show();
	$('.login_note_main').hide();
	$('#loginModal .modal-title').find('a').removeClass('pwd_active');
	$('#loginModal .modal-title').find('a').eq(0).addClass('pwd_active');
}
function msgLogin(){
	$('.login_pwd_main').hide();
	$('.login_note_main').show();
	$('#loginModal .modal-title').find('a').removeClass('pwd_active');
	$('#loginModal .modal-title').find('a').eq(1).addClass('pwd_active');
}